#!/usr/bin/perl
use strict;
use CGI;
use Fcntl;
use GDBM_File;
use Net::SMTP;

# ここは環境に合わせてください
my $path = "/home/cocteau/oss/maido/";
my $relay_server = "localhost";
my $relay_port = 25;

my $dbfile = $path . "maido.gdbm";
#my $sendmail = '/usr/sbin/sendmail';

my $delim = "\t";

my @mail;
my $in_received_header = 0;
my $received_header_checked = 0;
my $original_to = '';
my $debug = '';
while(<STDIN>){
  if ($received_header_checked == 0) {
    if ($_ =~ /^Received:/i) {
      $in_received_header = 1;
      $received_header_checked = 1;
    }
  }
  if ($in_received_header == 1 && $_ =~ /^\s/) {
    my $tmp = $_;
    $tmp =~ s/^\s+//;
    if ($tmp =~ /^for/) {
      my @tmps = split(/\s/, $tmp);
      $original_to = $tmps[1];
      $original_to =~ s/;$//;
      $original_to =~ s/\>$//;
      $original_to =~ s/^\<//;
      $in_received_header = 0;
    }
  }

  if ($_ =~ /^return-path:/i) {
    push @mail, "Return-Path: <$original_to>\r\n";
  } else {
    push @mail, $_;
  }
}

my ($local, $domain) = split(/\@/, $original_to);

tie my %db, 'GDBM_File', "$dbfile", O_RDWR|O_CREAT, 0666 or die;
my ($count, @emails) = split(/$delim/, $db{$local});
$count = int($count) + 1;
$db{$local} = $count . $delim . join($delim, @emails);
untie %db;

my $eml = '';
my $in_eml_header = 1;
foreach(@mail) {
  if ($_ eq "\r\n") {
    $in_eml_header = 0;
  }
  if ($in_eml_header == 1 && /^Subject:/i) {
    my $tmp = $_;
    $tmp =~ s/^Subject:/ /i;
    $tmp =~ s/\[$local:.+\]\s//g;
    $tmp =~ s/\[$local:.+\]//g;
    $tmp =~ s/\s//;
    $eml .= "Subject: [" . $local . ":" . sprintf("%05d", $count) . "]\r\n" . $tmp;
  } else {
    $eml .= $_;
  }
}

# 処理済みのメールをリレーする
my $smtp = Net::SMTP->new($relay_server, Port=>$relay_port);
$smtp->mail($original_to);
foreach my $to (@emails) {
  $smtp->to($to);
}
$smtp->data();
$smtp->datasend($eml);
$smtp->datasend();
$smtp->quit();

#if (open(*MAIL, "|$sendmail " . join(" ", @emails))) {
#  print MAIL $eml;
#  close(MAIL);
#}

exit 0;
